﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace WindowsApplication2
{
    //[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            //this.WB_Top.AllowWebBrowserDrop = false;
            //this.WB_Top.IsWebBrowserContextMenuEnabled = false;
            //this.WB_Top.WebBrowserShortcutsEnabled = false;
            //this.WB_Top.ScriptErrorsSuppressed = true;
            this.WB_Top.ObjectForScripting = this;
        }
        private void WB_FOOT_NewWindow(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
        private void WB_FOOT_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (this.bWinGo
                && this.WB_FOOT.DocumentText.Trim().ToLower().LastIndexOf(this.sHtmlEnd) != -1)
            {
                this.bWinGo = false;
                this.document = this.WB_FOOT.Document;                
                this.UF_Callback_WinGo(this.sWbFootUrl);
                //System.Windows.Forms.MessageBox.Show(this.sWbFootUrl + " 加载完成!");
                //System.Windows.Forms.MessageBox.Show(this.WB_FOOT.DocumentText);
            }
            else if (this.bWinNew
                && this.WB_FOOT.Url.OriginalString.ToString() != this.sWbFootUrl
                && this.WB_FOOT.DocumentText.Trim().ToLower().LastIndexOf(this.sHtmlEnd) != -1)
            {
                this.bWinNew = false;
                this.document = this.WB_FOOT.Document;
                this.UF_Callback_WinNew(this.WB_FOOT.Url.OriginalString.ToString());
            }
        }
        private void inject()
        {
            this.Script(this.document,"function confirm(){return true;};function alert(){};");
        }
        //
        // Script
        //
        public void Script(WebBrowser Wb, String Script)
        {
            this.Script(Wb.Document, Script);
            //Wb.Url = new System.Uri("javascript:" + Script, System.UriKind.Absolute);
        }
        public void Script(HtmlDocument Dom, String Script)
        {
            mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)Dom.Window.DomWindow;
            win.execScript(Script, "Javascript");
        }
        //
        //Select 
        //
        public void UF_Event(String Elem, String Event)
        {
            this.Script(this.document, Elem + "." + Event + "();");
        }
        public void UF_SetValue(String Elem, String attribute, String value)
        {
            System.Windows.Forms.MessageBox.Show(value);
            this.Script(this.document,Elem + ".setAttribute('" + attribute + "','" + value + "');");
        }
        //回调WB_TOP
        public void UF_Callback_WinGo(String address)
        {
            this.WB_Top.Document.InvokeScript("exeCallback", new Object[] { address,"WinGo" });
            this.inject();
        }
        public void UF_Callback_WinNew(String address)
        {
            this.WB_Top.Document.InvokeScript("exeCallback", new Object[] { address,"WinNew" });
            this.inject();
        }
        //用户定义函数
        public void UF_FootOpen()
        {
           this.splitContainer1.Panel2Collapsed  = false;
        }
        public void UF_FootClose()
        {
            this.UF_Clear();
            this.splitContainer1.Panel2Collapsed = true;
        }
        public void UF_Clear()
        {
            this.WB_FOOT.Url = new System.Uri("about:blank", System.UriKind.Absolute);
        }
        public void UF_WinNew(String Str)
        {
            Str = Str.ToLower();
            this.sHtmlEnd = Str;
            this.UF_WinNew();
        }
        public void UF_WinNew()
        {
            this.bWinNew = true;
        }
        public void UF_WinGo(String address)
        {
            this.UF_WinGo(address,"</html>");
        }
        public void UF_WinGo(String address,String Html)
        {
            if (String.IsNullOrEmpty(address)) return;
            if (address.Equals("about:blank")) return;
            if (!address.StartsWith("http://")) address = "http://" + address;
            try
            {
                if (this.splitContainer1.Panel2Collapsed)
                {
                    UF_FootOpen();
                }
                this.WB_FOOT.Url = new System.Uri(address, System.UriKind.Absolute);
                this.sWbFootUrl  = address;
                this.sHtmlEnd    = Html;
                this.bWinGo      = true;
            }catch (System.UriFormatException)
            {
                return;
            }
        }      
        //by yiping
        private HtmlDocument document;
        private Boolean bWinGo = false;
        private Boolean bWinNew = false;
        private String sWbFootUrl = null;
        private String sHtmlEnd = null;

        
    }
}